import copy
import re

from ninja import Router

from apps.commom.package import AgentPackageSerializer, VipPackageSerializer
from apps.manager.package.schemas import AddAgentPackageSchema, AddVipPackageSchema
from apps.schemas import R
from models.models import AgentPackage, VipPackage, AgentChannelShip
from utils.auth import ManagerAuthBearer
from utils.decorators import handle_exception

manage_package_router = Router(tags=["套餐管理"], auth=ManagerAuthBearer())


@manage_package_router.get("/agent/packages", summary="获取代理套餐列表")
@handle_exception("获取代理套餐列表失败")
def get_agent_packages(request):
    """
    获取代理套餐列表
    """
    agent_packages = AgentPackage.objects.all()
    return R.ok(data=AgentPackageSerializer(agent_packages, many=True).data)


@manage_package_router.get("/agent/package/{package_id}", summary="获取代理套餐详情")
@handle_exception("获取代理套餐详情失败")
def get_agent_package(request, package_id: int):
    """
    获取代理套餐详情
    """
    agent_package = AgentPackage.objects.get(id=package_id)
    return R.ok(data=AgentPackageSerializer(agent_package).data)


@manage_package_router.post("/agent/package", summary="添加代理套餐")
@handle_exception("添加代理套餐失败")
def add_agent_package(request, data: AddAgentPackageSchema):
    """
    添加代理套餐
    """
    number_re = re.compile(r'^1[3-9]\d{9}$')
    if not number_re.search(data.phone_number):
        return R.fail("手机号格式错误")
    data_dict = data.dict()
    data_copy = copy.deepcopy(data_dict)
    del data_copy["channel"]
    obj = AgentPackage.objects.create(**data_copy)
    obj.channel.set(data.channel)
    return R.ok(msg="添加成功")


@manage_package_router.delete("/agent/package/{package_id}", summary="删除代理套餐")
@handle_exception("删除代理套餐失败")
def delete_agent_package(request, package_id: int):
    """
    删除代理套餐
    """
    if not AgentPackage.objects.filter(id=package_id).exists():
        return R.fail("套餐不存在")
    AgentPackage.objects.get(id=package_id).delete()
    return R.ok(msg="删除成功")


@manage_package_router.put("/agent/package/{package_id}", summary="更新代理套餐")
@handle_exception("更新代理套餐失败")
def update_agent_package(request, package_id: int, data: AddAgentPackageSchema):
    """
    更新代理套餐
    """
    agent = AgentPackage.objects.get(id=package_id)
    agent.name = data.name
    agent.water_card_number = data.water_card_number
    agent.price = data.price
    if 3 in data.channel:
        if data.username is None:
            return R.fail("联系人信息不能为空")
        agent.username = data.username
        agent.phone_number = data.phone_number
        agent.address = data.address
    agent.save()
    AgentChannelShip.objects.filter(package=agent).delete()
    for channel in data.channel:
        AgentChannelShip.objects.create(channel_id=channel, package=agent)
    return R.ok(msg="更新成功")


@manage_package_router.get("/vip/packages", summary="获取VIP套餐列表")
@handle_exception("获取VIP套餐列表失败")
def get_vip_packages(request):
    """
    获取VIP套餐列表
    """
    return R.ok(data=VipPackageSerializer(VipPackage.objects.all(), many=True).data)


@manage_package_router.get("/vip/package/{package_id}", summary="获取VIP套餐详情")
@handle_exception("获取VIP套餐详情失败")
def get_vip_package(request, package_id: int):
    """
    获取VIP套餐
    """
    vip = VipPackage.objects.get(id=package_id)
    return R.ok(data=VipPackageSerializer(vip).data)


@manage_package_router.post("/vip/package", summary="添加VIP套餐")
@handle_exception("添加VIP套餐失败")
def add_vip_package(request, data: AddVipPackageSchema):
    """
    添加VIP套餐
    """
    VipPackage.objects.create(**data.dict())
    return R.ok(msg="添加成功")


@manage_package_router.delete("/vip/package/{package_id}", summary="删除VIP套餐")
@handle_exception("删除VIP套餐失败")
def delete_vip_package(request, package_id: int):
    """
    删除VIP套餐
    """
    if not VipPackage.objects.filter(id=package_id).exists():
        return R.fail("套餐不存在")
    VipPackage.objects.get(id=package_id).delete()
    return R.ok(msg="删除成功")


@manage_package_router.put("/vip/package/{package_id}", summary="更新VIP套餐")
@handle_exception("更新VIP套餐失败")
def update_vip_package(request, package_id: int, data: AddVipPackageSchema):
    """
    更新VIP套餐
    """
    VipPackage.objects.filter(pk=package_id).update(**data.dict())
    return R.ok(msg="更新成功")

